var generator = require('../app/generator/generator');

module.exports = function(app, passport, notify, dataAuth, connection, auth) {
	//curd-demo///////////
	var users = require('../app/controller/users');
	app.get('/api/test/test', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		users.select(req, res, connection);
	}); //test notify
	app.get('/api/user', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		users.select(req, res, connection);
	}); //查询用户信息
	app.post('/api/user', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		users.addUser(req, res, connection);
	}); //增加用户信息
	app.put('/api/user', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		users.updateOne(req, res, connection);
	}); //修改用户信息
	app.del('/api/user', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		users.deleteOne(req, res, connection);
	}); //删除指定id的用户信息
	app.get('/api/user/:name', function(req, res) {
		users.selectName(req, res, connection);
	}); //根据name查询用户信息
	app.put('/api/user_info', function(req, res) {
		users.changePwd(req, res, connection);
	}); //修改用户信息
	app.get('/api/getResource', function(req, res) {
		users.getResource(req, res, connection);
	}); //获取员工的角色对应的资源操作
	//用户登录
	app.get('/login', users.login);
	app.get('/signup', users.signup);
	app.get('/logout', users.logout);
	app.post('/username', function(req, res) {
		users.check(req, res, connection);
	});

	//---------------------------------role-----------------------
	var role = require('../app/controller/role');
	app.post('/api/role', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		role.addRole(req, res, connection);
	}); //添加角色
	app.get('/api/role', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		role.selectAll(req, res, connection);
	}); //查询所有的角色
	app.put('/api/role', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		role.updateRole(req, res, connection);
	}); //修改角色信息
	app.del('/api/role', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		role.deleteOne(req, res, connection);
	}); //删除角色
	app.get('/api/userRole/:id', function(req, res) {
		role.checkExist(req, res, connection);
	}); //查询角色是否被分配
	app.get('/api/role/user', function(req, res) {
		role.checkAllRole(req, res, connection);
	}); //根据userId查询其所有角色名

	var roleResource = require('../app/controller/roleResource');
	app.get('/api/roleresource', function(req, res) {
		roleResource.selectAll(req, res, connection);
	});
	app.post('/api/roleresource', function(req, res) {
		roleResource.addRoleResource(req, res, connection);
	});
	app.put('/api/roleresource', function(req, res) {
		roleResource.updateRoleResource(req, res, connection);
	});
	app.get('/api/roleresource/getResByUser', function(req, res) {
		roleResource.getResByUser(req, res, connection);
	}); //根据当前登录用户得到用户资源

	//---------------------------------org----------------------
	var org = require('../app/controller/organization');
	app.get('/api/organizations/getOrgTree', function(req, res) {
		org.getOrgTree(req, res, connection);
	}); //查询机构树
	app.post('/api/organization', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		org.addOrg(req, res, connection);
	}); //添加机构
	app.put('/api/organization', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		org.updateOrg(req, res, connection);
	}); //更新机构信息
	app.get('/api/organizations/checkOrgName', function(req, res) {
		org.checkOrgName(req, res, connection);
	}); //校验机构名称是否重复
	app.get('/api/organizations/getParents/:orgId', function(req, res) {
		org.getParents(req, res, connection);
	}); //根据机构id得到机构的父机构
	app.get('/api/organizations/getSysName/:orgId', function(req, res) {
		org.getSysName(req, res, connection);
	});//根据机构id得到体系名称
	app.get('/api/organizations/selOrg/:orgId', function(req, res) {
		org.selOrgById(req, res, connection);
	}); //根据id查询机构
	app.get('/api/organizations/checkSystem/:sysid', function(req, res) {
		org.checkSystem(req, res, connection);
	});
	app.get('/api/organizations/orgCheck/:orgId', function(req, res) {
		org.checkOrg(req, res, connection);
	});
	app.del('/api/organization', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		org.delOrg(req, res, connection);
	});
	app.get('/api/organizations/depart', function(req, res) {
		org.getDepart(req, res, connection);
	}); //获取部门信息
	app.get('/api/organizations/checkRoot', function(req, res) {
		org.checkRoot(req, res, connection);
	}); //校验是否存在根节点
	app.get('/api/organizations/selOrgByUser', function(req, res) {
		org.selOrgByUser(req, res, connection);
	}); //根据当前用户查询负责机构（部门经理）


	var system = require('../app/controller/system');
	app.get('/api/system', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		system.selectSys(req, res, connection);
	}); //查询所有体系
	app.get('/api/system/checkSysName', function(req, res) {
		system.checkSysName(req, res, connection);
	}); //校验体系名称是否重复
	app.post('/api/system', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		system.addSys(req, res, connection);
	}); //添加体系
	app.put('/api/system', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		system.updateSys(req, res, connection);
	}); //更新体系
	app.del('/api/system', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		system.delSystem(req, res, connection);
	});

	var item = require('../app/controller/item');
	app.post('/api/item', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		item.addItem(req, res, connection);
	}); //添加选项信息
	app.put('/api/item', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		item.updateItem(req, res, connection);
	}); //更新选项信息
	app.get('/api/item', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		item.selAll(req, res, connection);
	});
	app.get('/api/item/checkItem', function(req, res) {
		item.checkItem(req, res, connection);
	}); //校验选项信息
	app.del('/api/item', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		item.delItem(req, res, connection);
	});
	app.get('/api/item/search', function(req, res) {
		item.selectAll(req, res, connection);
	}); //根据类别查询item信息
	app.post('/api/item/access', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		item.accessChange(req, res, connection);
	}); //更新开放权限
	app.get('/api/item/access', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		item.getAccess(req, res, connection);
	}); //查看开放权限


	var performance = require('../app/controller/performance');
	app.post('/api/performance', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		performance.savePer(req, res, connection);
	}); //保存员工填写的绩效及绩效项
	app.put('/api/performance', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		performance.updateEmpPerf(req, res, connection);
	}) //更新员工填写的绩效及绩效项
	app.get('/api/performance/selEmpPerfs', function(req, res) {
		performance.selEmpPerfs(req, res, connection);
	}); //查询当前员工的所有绩效成绩
	app.get('/api/performance/selMonthPerf/:timeStr', function(req, res) {
		performance.selMonthPerf(req, res, connection);
	}); //查询当前员工某月的绩效信息
	app.get('/api/performance/singlePerf/:timeStr', function(req, res) {
		performance.singlePerf(req, res, connection);
	}); //查询当前员工某一月份绩效成绩
	app.del('/api/performance/delPerfTerm', function(req, res) {
		performance.delPerfTerm(req, res, connection);
	}); //删除绩效项
	app.get('/api/performance/selMonthPerfsByAss', function(req, res) {
		performance.selMonthPerfsByAss(req, res, connection);
	}); //查询考核人为当前登录用户的当月绩效信息
	app.get('/api/performance/selPerfsByAss', function(req, res) {
		performance.selPerfsByAss(req, res, connection);
	}); //根据考核人查询绩效列表
	app.get('/api/performance/selPerfById/:perfId', function(req, res) {
		performance.getPerfById(req, res, connection);
	});
	app.get('/api/performance/selHisPerfs', function(req, res) {
		performance.selHisPerfs(req, res, connection);
	}); //查询当前登录用户的历史绩效成绩
	app.get('/api/performance/checkExist', function(req, res) {
		performance.checkExist(req, res, connection);
	}); //校验同一月份的绩效是否存在
	app.get('/api/performance/checkAssignmentIdExit', function(req, res) {
		performance.checkAssignmentIdExit(req, res, connection);
	}); //校验未被审核的绩效是否存在指定的
	app.put('/api/performance/state', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		performance.changeState(req, res, connection);
	}); //确认绩效信息
	app.put('/api/performance/return', function(req, res) {
		performance.returnPerf(req, res, connection);
	}); //回退绩效信息
	app.get('/api/performance_term/:id', function(req, res) {
		performance.selectPerTerm(req, res, connection);
	}); //根据id查询绩效及所属所有绩效项信息
	app.put('/api/performance_term', function(req, res) {
		performance.changeWeight(req, res, connection);
	});
	app.get('/api/performance/selPerfDetail', function(req, res) {
		performance.selPerfDetail(req, res, connection);
	});


	//--------------------------file------------------------------------
	var file = require('../app/controller/file');
	app.post('/api/files', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		file.upload(req, res, connection);
	});
	app.get('/files/download', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		file.download(req, res, connection);
	});
	app.get('/api/files', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		file.show(req, res, connection);
	});
	app.del('/api/files', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		file.destroy(req, res, connection);
	});

	//-------------------------employee---------------------------------
	var employee = require('../app/controller/employee');

	app.post('/api/employee', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		employee.addEmp(req, res, connection);
	}); //添加员工
	app.get('/api/employee/all', function(req, res) {
		employee.selectEmp(req, res, connection);
	}); //查询所有员工信息
	app.get('/api/employee/selByRole', function(req, res) {
		employee.selEmpByRole(req, res, connection);
	}); //查询有审核权限的员工
	app.get('/api/employee/selAssEmpByName', function(req, res) {
		employee.selEmpAssByName(req, res, connection);
	});
	app.get('/api/employees/:id', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		employee.selectOneEmp(req, res, connection);
	}); //查询指定员工信息
	app.del('/api/employees/:id', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		employee.delEmp(req, res, connection);
	}); //删除指定员工
	// app.del('/api/employee/del/list',function(req,res){
	// 	employee.delListEmp(req, res, connection);
	// }); //批量删除员工
	app.get('/api/employee/getEmpByName', function(req, res) {
		employee.getEmpByName(req, res, connection);
	}); //通过员工名字得到员工id
	app.put('/api/employees/:id', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		employee.updateEmp(req, res, connection);
	});
	app.get('/api/employee', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		employee.searchByKey(req, res, connection);
	}); //通过输入条件搜索员工信息
	app.get('/api/employee/getEmpByUser', function(req, res) {
		employee.selEmpByUser(req, res, connection);
	}); //根据用户信息得到员工信息
	app.get('/api/emplyee/selEmpByOrg', function(req, res) {
		employee.selEmpByOrg(req, res, connection);
	});

	//--------------------------record----------------------
	var record = require('../app/controller/record');
	app.get('/api/record/:id', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		record.selectByEmployeeId(req, res, connection);
	}); //查询指定id的员工的所有记录信息
	// app.get('/api/record/lists/:key/:id', function(req, res) {
	// 	record.selectByKeyAndId(req, res, connection);
	// }); //根据搜索条件查询记录信息

	//--------------------------region--------------------
	var region = require('../app/controller/region');
	app.get('/api/region/provinces', function(req, res) {
		region.selectPro(req, res, connection);
	}); //获取所有的省份
	app.get('/api/region/citiy/:id', function(req, res) {
		region.selectCityByProId(req, res, connection);
	}); //根据省id获取所有的城市
	app.get('/api/regions/cities', function(req, res) {
		region.selectCities(req, res, connection);
	}); //获取所有的城市
	app.get('/api/region/province/:city', function(req, res) {
		region.selectProByCityName(req, res, connection);
	}); //根据城市名获取所在省

	//------------------------statistics-------------------
	var statistics = require('../app/controller/statistics');
	app.get('/api/statistics', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res, next) {
		dataAuth.pass(req, res, next, connection)
	}, function(req, res) {
		statistics.selectStatis(req, res, connection);
	}); //根据搜索条件分页
	app.get('/api/statistics/exportExcel', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		statistics.exportExcel(req, res, connection);
	}); //导出统计表
	app.get('/api/statistics/performance/statisPerf', function(req, res, next) {
		dataAuth.pass(req, res, next, connection)
	}, function(req, res) {
		statistics.statisPerf(req, res, connection);
	}); //统计所有未填写及未审核某月月绩效的员工
	app.get('/api/statistics/organization/countRatio', function(req, res, next) {
		dataAuth.pass(req, res, next, connection)
	}, function(req, res) {
		statistics.selRatio(req, res, connection);
	});
	app.get('/statistics/analyze',function(req, res, next) {
		notify.pass(req, res, next, connection);
	},function(req, res) {
		statistics.analyzePerf(req, res, connection);
	});//绩效统计


	//-----------------------resources---------------------
	var resource = require('../app/controller/resource');
	app.get('/api/resource', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		resource.selectAll(req, res, connection);
	}); //查询资源
	app.post('/api/resource', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		resource.addResource(req, res, connection);
	}); //增加资源
	app.put('/api/resource', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		resource.updateResource(req, res, connection);
	}); //修改资源
	app.del('/api/resource', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, function(req, res) {
		resource.deleteResource(req, res, connection);
	});

	//-------------------------log-------------------------
	var log = require('../app/controller/log');
	app.get('/api/log', function(req, res) {
		log.selectLog(req, res, connection);
	}); // 根据itemId和type查询日志信息

	app.get('/api/log/:id', function(req, res) {
		log.selectLogById(req, res, connection);
	}); // 根据itemId查询日志信息
	app.post('/users/session', passport.authenticate('local', {
		successRedirect: '/desktop',
		failureRedirect: '/login',
		failureFlash: 'Invalid email or password.'
	}), users.session);

	app.get('/', auth.requiresLogin, function(req, res) {
		res.redirect('/login');
	});

	app.get('/desktop', auth.requiresLogin, function(req, res) {
		res.send(generator.generate('desktop', {
			username: req.user.userName,
			state: req.user.state,
			name: req.user.name
		}));
	});

	app.get('/staff', auth.requiresLogin, function(req, res) {
		res.send(generator.generate('staff', {
			username: req.user.userName,
			name: req.user.name,
			hisPage: req.query.hisPage,
			keywords: req.query.keywords
		}));
	});

	app.get('/usermanage', auth.requiresLogin, function(req, res) {
		res.send(generator.generate('usermanage', {
			username: req.user.userName,
			name: req.user.name
		}));
	});

	app.get('/rolemanage', auth.requiresLogin, function(req, res) {
		res.send(generator.generate('rolemanage', {
			username: req.user.userName,
			name: req.user.name
		}));
	});

	app.get('/resourcemanage', auth.requiresLogin, function(req, res) {
		res.send(generator.generate('resourcemanage', {
			username: req.user.userName,
			name: req.user.name
		}));
	});

	app.get('/organization', auth.requiresLogin, function(req, res) {
		res.send(generator.generate('organization', {
			username: req.user.userName,
			name: req.user.name
		}));
	});

	app.get('/system', auth.requiresLogin, function(req, res) {
		res.send(generator.generate('system', {
			username: req.user.userName,
			name: req.user.name
		}));
	});

	app.get('/statistics', auth.requiresLogin, function(req, res) {
		res.send(generator.generate('statistics', {
			username: req.user.userName,
			name: req.user.name,
			hisPage: req.query.hisPage,
			key: req.query.key
		}));
	});
	app.get('/analyze', auth.requiresLogin, function(req, res) {
		res.send(generator.generate('analyze', {
			username: req.user.userName,
			name: req.user.name,
			key: req.query.key
		}));
	});
	app.get('/statistics/perfDetails', auth.requiresLogin, function(req, res) {
		res.send(generator.generate('perfDetails', {
			username: req.user.userName,
			name: req.user.name,
			key: req.query.key
		}));
	});
	

	app.get('/staff/edit', auth.requiresLogin, function(req, res) {
		res.send(generator.generate('staffEdit', {
			username: req.user.userName,
			userId: req.user.id,
			employeeId: req.query.id,
			cpage: req.query.cpage,
			name: req.user.name,
			keywords: req.query.keywords
		}));
	});
	app.get('/staff/log', auth.requiresLogin, function(req, res) {
		res.send(generator.generate('staffEdit', {
			username: req.user.userName,
			userId: req.user.id,
			employeeId: req.query.employeeId,
			versionId: req.query.logId,
			name: req.user.name
		}));
	});

	app.get('/staff/new', auth.requiresLogin, function(req, res) {
		res.send(generator.generate('staffAdd', {
			username: req.user.userName,
			userId: req.user.id,
			name: req.user.name
		}));
	});

	app.get('/performanceAdd', auth.requiresLogin, function(req, res) {
		res.send(generator.generate('performanceAdd', {
			username: req.user.userName,
			year: req.query.year,
			month: req.query.month,
			access: req.query.access,
			employeeId: req.user.employeeId,
			name: req.user.name
		}));
	}); //我的绩效

	// app.get('/performanceAdd/:year/:month', auth.requiresLogin, function(req, res) {
	// 	res.send(generator.generate('performanceAdd', {
	// 		year: req.params.year,
	// 		month: req.params.month,
	// 		username: req.user.userName
	// 	}));
	// });

	app.get('/performance', auth.requiresLogin, function(req, res) {
		res.send(generator.generate('performance', {
			userId: req.user.id,
			username: req.user.userName,
			name: req.user.name,
			year: req.query.year,
			month: req.query.month
		}));
	});

	// app.get('/performance/:year/:month', auth.requiresLogin, function(req, res) {
	// 	res.send(generator.generate('performance', {
	// 		year: req.params.year,
	// 		month: req.params.month,
	// 		username: req.user.userName
	// 	}));
	// });

	app.get('/item', auth.requiresLogin, function(req, res) {
		res.send(generator.generate('item', {
			userId: req.user.id,
			username: req.user.userName,
			name: req.user.name
		}));
	});
	app.get('/accessControl', auth.requiresLogin, function(req, res) {
		res.send(generator.generate('accessControl', {
			userId: req.user.id,
			username: req.user.userName,
			name: req.user.name
		}));
	});

	app.get('/performanceAudit', auth.requiresLogin, function(req, res) {
		res.send(generator.generate('performanceAudit', {
			userId: req.user.id,
			username: req.user.userName,
			name: req.user.name,
			hisPage: req.query.hisPage,
			hisPage2: req.query.hisPage2,
			key: req.query.key
		}));
	}); //我的考核

	app.get('/performanceAudit/staffAudit', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, auth.requiresLogin, function(req, res) {
		res.send(generator.generate('staffAudit', {
			userId: req.user.id,
			username: req.user.userName,
			name: req.user.name,
			perfId: req.query.perfId,
			employeeId: req.user.employeeId,
			termState: req.query.termState,
			cpage: req.query.cpage
		}));
	}); //员工绩效审核

	app.get('/performanceAudit/staffManagerAudit', function(req, res, next) {
		notify.pass(req, res, next, connection);
	}, auth.requiresLogin, function(req, res) {
		res.send(generator.generate('staffManagerAudit', {
			userId: req.user.id,
			username: req.user.userName,
			name: req.user.name,
			cpage: req.query.cpage,
			perfId: req.query.perfId,
			key: req.query.key
		}));
	}); //员工绩效审核

	app.get('/performanceAudit/staffPerformance/:perfId', auth.requiresLogin, function(req, res) {
		res.send(generator.generate('staffPerformance', {
			userId: req.user.id,
			username: req.user.userName,
			name: req.user.name,
			perfId: req.params.perfId,
			cpage: req.query.cpage,
			key: req.query.key
		}));
	});

	app.get('/organization/orgEmp', auth.requiresLogin, function(req, res) {
		res.send(generator.generate('orgEmp', {
			userId: req.user.id,
			username: req.user.userName,
			name: req.user.name,
			orgId: req.query.orgId
		}));
	});

	process.on('uncaughtException', function(err) {
		console.log(err);
		console.log(err.stack);

	});

};